package com.yang.qqclient.view;

import com.yang.qqclient.service.FileClientService;
import com.yang.qqclient.service.MessageClientService;
import com.yang.qqclient.service.UserClientService;
import com.yang.qqclient.utils.Utility;

/**
 * @Description: 客户端的菜单界面
 * @Author: YANG QIANG
 * @Time: 2022-12-19 22:25
 */
public class QQView {
    // 是否退出系统的表示
    private boolean loop = true;
    // 用户输入的操作
    private int key;
    // User业务对象
    private UserClientService userClientService = new UserClientService();
    // 用户消息的业务对象
    private MessageClientService messageClientService = new MessageClientService();
    // 文件发送业务对象
    private FileClientService fileClientService = new FileClientService();

    public static void main(String[] args) {
        new QQView().mainMenu();
        System.out.println("客户端退出系统");
    }

    /**
     * 主菜单
     */
    private void mainMenu(){

        while (loop){
            // 打印一级菜单
            printOneMenu();
            // 获取用户一级菜单的操作
            key = Utility.readInt(1);
            switch (key){
                case 1:
                    System.out.println("请输入用户ID:");
                    String userId = Utility.readString(50);
                    System.out.println("请输入密码:");
                    String pwd = Utility.readString(50);
                    // 构建user对象, 用于后面统一用
                    userClientService = new UserClientService(userId, pwd);
                    // 到服务端验证用户是否合法
                    if (userClientService.checkUser()){
                        // 显示系统二级菜单
                        System.out.println("==========欢迎用户: "+ userId + "===========");
                        // 打印二级菜单
                        printTwoMenu(userId);
                        while (loop){
                            // 获取用户二级菜单的操作
                            key = Utility.readInt(1);
                            switch (key){
                                case 1:
                                    // 编写一个方法来获取用户在线列表, 我们写到UserClientService
                                    userClientService.onlineFriendList();
                                    break;
                                case 2:
                                    System.out.println("请输入想对大家说的话: ");
                                    String content1 = Utility.readString(100);
                                    // 编写一个方法发送给服务端
                                    messageClientService.sendMessageToAll(content1, userId);
                                    break;
                                case 3:
                                    System.out.println("请输入想聊天的在线的用户名: ");
                                    String getterId = Utility.readString(50);
                                    System.out.println("请输入想说的话: ");
                                    String content = Utility.readString(100);
                                    // 编写一个方法发送消息, 编写一个聊天的服务类
                                    messageClientService.sendMessage(content, userId, getterId);
                                    break;
                                case 4:
                                    System.out.println("请输入你想把文件发送给的在线用户:");
                                    String getter = Utility.readString(50);
                                    System.out.println("请输入发送文件的路径:");
                                    String src = Utility.readString(100);
                                    System.out.println("请输入发送给对方文件保存的路径:");
                                    String dest = Utility.readString(100);
                                    fileClientService.sendFileToOne(src, dest, userId, getter);
                                    break;
                                case 9:
                                    // 编写一个退出客户端的方法
                                    userClientService.logout();
                                    loop = false;
                                    break;
                                default:
                                    System.out.println("输入错误,请重新输入");
                            }
                        }

                    }else {
                        System.out.println("登录服务器失败");

                    }
                    break;
                case 9:
                    System.out.println("退出成功");
                    loop = false;
                    break;
                default:
                    System.out.println("输入错误,请重新输入");
            }
        }
    }

    private void printOneMenu() {
        System.out.println("==========欢迎登录网络通信系统===========");
        System.out.println("\t\t 1 登录系统");
        System.out.println("\t\t 9 退出系统");
        System.out.println("请输入您的选择:");

    }

    private void printTwoMenu(String userId) {
        System.out.println("\n==========网络通信二级菜单(用户: "+ userId + ")===========");
        System.out.println("\t\t 1 显示在线用户列表");
        System.out.println("\t\t 2 群发消息");
        System.out.println("\t\t 3 私聊消息");
        System.out.println("\t\t 4 发送文件");
        System.out.println("\t\t 9 退出系统");
        System.out.println("请输入您的选择:");
    }
}
